package com.dhcc.bpm.modules.scf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.modules.scf.dto.ScfFileInfoDTO;
import com.dhcc.bpm.modules.scf.service.IScfFileInfoService;
import com.dhcc.bpm.modules.scf.vo.ScfFileInfoVO.ScfGetFileListVO;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件存储控制器
 * liuguoting
 * 2023-09-05
 */
@RestController
@AllArgsConstructor
public class ScfFileInfoController extends BladeController {
    private IScfFileInfoService scfFileInfoService;


    /**
     * 分页查询文件包列表
     */
    @GetMapping("/scfGetFilelist")
    // @Cacheable(value = "scfFileInfoListCache", key = "'scfFileInfoListCacheKey-' + #scfFileInfo.hashCode() + '-' + #query.hashCode()", unless = "#result == null")
    public R<IPage<ScfGetFileListVO>> page(ScfFileInfoDTO scfFileInfo, Query query) {
        return R.data(scfFileInfoService.selectScfFileInfoPage(Condition.getPage(query), scfFileInfo));
    }

    /**
     * 文件包上传
     */
    @PostMapping("/ScfAddFile")
    // @CacheEvict(value = "scfFileInfoListCache", allEntries = true)
    public R<Boolean> uploadFile(@RequestParam("subNum") String subNum, @RequestParam("userId") String userId, MultipartFile file) {
        return scfFileInfoService.uploadFile(subNum,userId,file);
    }

    /**
     * 根据文件ID删除文件包
     */
    @GetMapping("/ScfDelFile")
    // @CacheEvict(value = "scfFileInfoListCache", allEntries = true)
    public R<Boolean> deleteByFileNumS(@RequestParam("fileNum") String fileNum) {
        return scfFileInfoService.deleteByFileNumS(fileNum);
    }

    /**
     * 根据文件编号下载文件包(字节流下载接口)
     *
     * @param fileNum
     * @return
     */
    @GetMapping("/ScfDownloadFile")
    public ResponseEntity<byte[]> downloadFile(@RequestParam("fileNum") String fileNum) throws IOException {
        return scfFileInfoService.downloadFile(fileNum);
    }

    /**
     * 根据文件编号下载文件包（Url下载接口）
     *
     * @param fileNum
     * @return
     */
    @GetMapping("/ScfDownloadFileUrl")
    public R<String> getUrl(@RequestParam("fileNum") String fileNum) throws IOException {
        String url = scfFileInfoService.getUrl(fileNum);
        return R.data(url);
    }

    /**
     * 下载文件模板（Url下载接口）
     *
     * @return
     */
    @GetMapping("/scfTemplateDownload")
    public R<String> getUrl() throws IOException {
        String url = scfFileInfoService.getTemplateUrl();
        return R.data(url);
    }
}
